package com.example.bdmanagerapplication


import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.baidu.mapapi.map.BitmapDescriptorFactory
import com.baidu.mapapi.map.MapStatus
import com.baidu.mapapi.map.MapStatusUpdateFactory
import com.baidu.mapapi.map.MarkerOptions
import com.baidu.mapapi.map.MyLocationData
import com.baidu.mapapi.map.OverlayOptions
import com.baidu.mapapi.model.LatLng
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener
import com.baidu.mapapi.search.poi.PoiCitySearchOption
import com.baidu.mapapi.search.poi.PoiDetailResult
import com.baidu.mapapi.search.poi.PoiDetailSearchResult
import com.baidu.mapapi.search.poi.PoiIndoorResult
import com.baidu.mapapi.search.poi.PoiNearbySearchOption
import com.baidu.mapapi.search.poi.PoiResult
import com.baidu.mapapi.search.poi.PoiSearch
import com.example.bdmanagerapplication.MapUtil.launchAMap
import com.example.bdmanagerapplication.databinding.ActivityNearBinding
import com.google.gson.Gson


class NearActivity : AppCompatActivity(),CustomAdapter.CallBack {
    private val mBinding by lazy { ActivityNearBinding.inflate(layoutInflater) }
    private val mList = ArrayList<MyPoiInfo>()
    private val poiDao by lazy { AppDatabase.getDatabase(this).poiInfoDao() }
    private val mViewModel by lazy { ViewModelProvider(this)[PoiInfoViewModel::class.java] }
    private val entity by lazy { intent.getSerializableExtra("entity") as PoiEntity }
    private val adapter=CustomAdapter(mList,this)
    private var listener: OnGetPoiSearchResultListener = object : OnGetPoiSearchResultListener {
        override fun onGetPoiResult(p0: PoiResult?) {
            p0?.allPoi?.let {list->
                mList.clear()
                list.forEach {
                    val gson = Gson()
                    Log.d("测试数据",it.toString())
                    val myPoiInfo = gson.fromJson(gson.toJson(it), MyPoiInfo::class.java)
                    mList.add(myPoiInfo)
                }
                mViewModel.getChangeList(poiDao,mList)
            }
        }

        override fun onGetPoiDetailResult(p0: PoiDetailResult?) {

        }

        override fun onGetPoiDetailResult(p0: PoiDetailSearchResult?) {

        }

        override fun onGetPoiIndoorResult(p0: PoiIndoorResult?) {

        }


    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(mBinding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        Log.d("测试数据","${entity.latitude}")
        mBinding.bmapView.map.isMyLocationEnabled = true
        val point=LatLng(entity.latitude,entity.longitude)

        val mapStatus = MapStatus.Builder()
            .target(point) // 设置新的中心位置坐标
            .build()
        // 更新地图状态
        val mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus)
        mBinding.bmapView.map.setMapStatus(mapStatusUpdate)
//        val locData = MyLocationData.Builder()
//            .latitude(entity.latitude)
//            .longitude(entity.longitude).build()
//        mBinding.bmapView.map.setMyLocationData(locData)
        mark(point)
        val mPoiSearch = PoiSearch.newInstance()
        mPoiSearch.setOnGetPoiSearchResultListener(listener)

        mPoiSearch.searchNearby( PoiNearbySearchOption()
            .location(point)
            .radius(10000)
            .keyword("公司")
            .pageNum(0))
        mBinding.mNearRV.layoutManager= LinearLayoutManager(this)
        mBinding.mNearRV.adapter=adapter
        mViewModel.isPoiExistsResult.observe(this){
            mList.clear()
            mList.addAll(it)
            adapter.notifyDataSetChanged()
        }
    }

    fun mark(point:LatLng) {

        //定义Maker坐标点
        //val point = LatLng(39.963175, 116.400244)

//构建Marker图标
        val bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.nail_icon)

//构建MarkerOption，用于在地图上添加Marker
        val option: OverlayOptions = MarkerOptions()
            .position(point)
            .icon(bitmap)

//在地图上添加Marker，并显示
        mBinding.bmapView.map.addOverlay(option)
    }

    override fun addItemClick(data: MyPoiInfo, position: Int) {

    }

    override fun itemNearClick(data: MyPoiInfo, position: Int) {

    }

    override fun navigateClick(data: MyPoiInfo, position: Int) {
        launchAMap(this,data.location.latitude,data.location.longitude,data.name)
    }
}